package com.sh.data.engine.domain.datadev.offline.service.impl;

import com.sh.data.engine.domain.datadev.offline.service.FreemarkerService;
import freemarker.template.Template;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import java.util.Map;

/**
 * @author tyf @Description:
 * @date 2020-06-1910:14
 */
@Service
@Slf4j
public class FreemarkerServiceImpl implements FreemarkerService {

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public String present(String templateName, Map<String, Object> map) {

        Template template = null;
        try {
            template = freeMarkerConfigurer.getConfiguration().getTemplate(templateName);
            String text = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            text = text.replaceAll("\r\n", "\n");
            return text;
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }

        return null;
    }
}
